package com.iot08.api.service;

import com.iot08.common.entity.app.OpenidConfig;
import com.iot08.common.service.base.BaseService;
import com.iot08.common.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

/**
 * @version 1.0
 * @Description:
 * @author: Rock
 * @time: 2021/1/7 15:23
 */
@Slf4j
@Service
public class OpenidConfigService extends BaseService<OpenidConfig> {

    /**
     * 检查且更新 如果没有此openid会进行更新
     * @param openid
     * @param mobileType
     * @param sysVersion
     * @param version
     * @param iosProduction
     * @param packageName
     * @param registrationID
     * @param oldRegistrationID
     * @time: 2021/1/7 17:08
     * @author: Rock
     * @version 1.0
     */
    public void checkUpdate(String openid,
                            String mobileType,
                            String sysVersion,
                            String version,
                            String iosProduction,
                            String packageName,
                            String registrationID,
                            String oldRegistrationID) {
        OpenidConfig uc = this.findById(openid);
        if (uc == null) {
            uc = new OpenidConfig();
            uc.setId(openid);
            uc.setOpenid(openid);
            uc.setMobileType(mobileType);
            uc.setSysVersion(sysVersion);
            uc.setVersion(version);
            if(iosProduction != null && iosProduction.contains("dev")){
                uc.setIosProduction(0);
            }else{
                uc.setIosProduction(1);
            }

            uc.setPackageName(packageName);
            uc.setRegistrationID(registrationID);
            uc.setOldRegistrationID(oldRegistrationID);
            uc.setTime(System.currentTimeMillis() / 1000);
            try {
                //TODO 会出现主键_id重复的情况
                this.save(uc);
            } catch (Exception e) {
                e.printStackTrace();
                log.error("checkUpdate save Exception. uc = {}", uc);
            }
        } else {
            Update update = new Update();
            update.set("openid", openid);
            update.set("mobile_type", mobileType);
            update.set("sysVersion", sysVersion);
            update.set("version", version);
            update.set("updateTime", System.currentTimeMillis() / 1000);
            if (StringUtil.isNotBlank(packageName)) {
                update.set("packageName", packageName);
            }
            if (iosProduction != null) {
                if(iosProduction.contains("dev")){
                    update.set("ios_production", 0);
                }else{
                    update.set("ios_production", 1);
                }

            }
            if (registrationID != null) {
                update.set("registrationID", registrationID);
            }
            if (oldRegistrationID != null) {
                update.set("oldRegistrationID", oldRegistrationID);
            }
            this.updateById(openid, update);
        }
    }

}
